package com.aliengen.thingple.driverapp.data.local;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;
import com.aliengen.thingple.driverapp.network.api.exception.FieldNotFoundException;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.List;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
@Table(name = "Customer")
public class CustomerModel extends Model implements Serializable {
    @Column(name = "customer_id",index = true,unique = true)
    public int customer_id;

    @Column(name = "reference")
    public String reference;

    @Column(name = "name")
    public String name;

    @Column(name = "address")
    public String address;

    @Column(name = "contact")
    public String contact;

    @Column(name = "mobile")
    public String mobile;

    @Column(name = "mail")
    public String mail;

    @Column(name = "notes")
    public String notes;

    public CustomerModel() {
        super();
    }

    public CustomerModel(JSONObject json) throws FieldNotFoundException
    {
        super();
        if (!(json.has("customer_reference")
                && json.has("customer_name")
                && json.has("customer_id")
                && json.has("customer_address")
                && json.has("contact")
                && json.has("mobile")
                && json.has("mail")
                && json.has("notes"))) {
            throw new FieldNotFoundException("Missing fields!");
        }

        try {
            this.customer_id = json.getInt("customer_id");
            this.reference = json.getString("customer_reference");
            this.name = json.getString("customer_name");
            this.address = json.getString("customer_address");
            this.contact = json.getString("contact");
            this.mobile = json.getString("mobile");
            this.mail = json.getString("mail");
            this.notes = json.getString("notes");
        } catch(JSONException e)
        {
            throw new FieldNotFoundException(e);
        }
    }

    public static CustomerModel getOrSet(JSONObject customer) throws FieldNotFoundException
    {
        if(!customer.has("customer_id"))
        {
            throw new FieldNotFoundException("Missing customer fields!");
        }

        try {
            CustomerModel customer_object = new Select().from(CustomerModel.class).where("customer_id = ?", customer.getInt("customer_id")).executeSingle();

            if(customer_object != null)
            {
                if(customer.has("customer_reference") && customer_object.reference.compareTo(customer.getString("customer_reference")) != 0)
                {
                    customer_object.reference = customer.getString("customer_reference");
                    customer_object.save();
                }

                if(customer.has("customer_name") && customer_object.name.compareTo(customer.getString("customer_name")) != 0)
                {
                    customer_object.name = customer.getString("customer_name");
                    customer_object.save();
                }

                if(customer.has("customer_address") && customer_object.address.compareTo(customer.getString("customer_address")) != 0)
                {
                    customer_object.address = customer.getString("customer_address");
                    customer_object.save();
                }

                if(customer.has("contact") && customer_object.contact.compareTo(customer.getString("contact")) != 0)
                {
                    customer_object.contact = customer.getString("contact");
                    customer_object.save();
                }

                if(customer.has("mobile") && customer_object.mobile.compareTo(customer.getString("mobile")) != 0)
                {
                    customer_object.mobile = customer.getString("mobile");
                    customer_object.save();
                }

                if(customer.has("mail") && customer_object.mail.compareTo(customer.getString("mail")) != 0)
                {
                    customer_object.mail = customer.getString("mail");
                    customer_object.save();
                }

                if(customer.has("notes") && customer_object.notes.compareTo(customer.getString("notes")) != 0)
                {
                    customer_object.notes = customer.getString("notes");
                    customer_object.save();
                }

                return customer_object;
            }

            customer_object = new CustomerModel(customer);
            customer_object.save();

            return customer_object;
        } catch(JSONException e) {
            throw new FieldNotFoundException(e.getMessage());
        }
    }

    public static List<CustomerModel> getCustomers()
    {
        return new Select().from(CustomerModel.class).orderBy("name ASC").execute();
    }

    public static void flush()
    {
        new Delete().from(CustomerModel.class).execute();
    }
}
